//
//  DiscountSmallImageTableViewCell.m
//  AroundTheWorld
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "DiscountSmallImageTableViewCell.h"
#import "DiscountModel.h"
#import "DiscountSmallImageCollectionViewCell.h"

@implementation DiscountSmallImageTableViewCell

//自定义初始化
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addView];
        self.smallArray = [NSMutableArray array];
    }
    return self;
}

- (void)addView
{
    self.contentView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0xCFCFCF);
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 30) / 2, 210);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
    
    collectionV.backgroundColor = [UIColor clearColor];
    collectionV.bounces = NO;
    [self.contentView addSubview:collectionV];
    
    //成为代理人
    collectionV.delegate = self;
    collectionV.dataSource = self;
    
    //注册
    [collectionV registerClass:[DiscountSmallImageCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.myCollectionV = collectionV;
}
//cell数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.smallArray.count;
}
//建cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DiscountSmallImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    DiscountModel *model = [self.smallArray objectAtIndex:indexPath.row];
    [cell.smallImageV sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@""]];
    cell.titleLabel.text = model.title;
    
    //字符串分割
    NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:@"[a-zA-Z.<,>,/]" options:0 error:NULL];
    NSString *string = model.price;
    NSString *price= [re stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
    
    cell.priceLabel.text = price;
    cell.priceoffLabel.text = model.priceoff;
    return cell;
}

- (void) setSmallArray:( NSMutableArray *) smallArray
{
    _smallArray = smallArray;
    [self.myCollectionV reloadData];
}

//点击触发方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DiscountModel *model =[self.smallArray objectAtIndex:indexPath.row];
    self.myBlock(model.discountId, indexPath.row);
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.myCollectionV.frame = self.contentView.frame;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
